<template>
  <div class="login">
    <div class="login-top">明亮后台管理系统</div>
    <el-tabs type="border-card" class="login-middle" stretch v-model="loginMethod">
      <el-tab-pane name="accountLogin">
        <template #label>
          <el-icon class="login-middle-icon"><UserFilled /></el-icon>
          <span>账号登录</span>
        </template>
        <el-form
          ref="accountLoginFormRef"
          :model="accountLoginForm"
          :rules="accountLoginRules"
          label-width="auto"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="accountLoginForm.username" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="accountLoginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
            />
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane name="mobileLogin">
        <template #label>
          <el-icon class="login-middle-icon"><Iphone /></el-icon>
          <span>手机登录</span>
        </template>
        <el-form ref="mobileLoginFormRef" :model="mobileLoginForm" :rules="mobileLoginRules">
          <el-form-item label="手机号" prop="mobileNumber">
            <el-input v-model="mobileLoginForm.mobileNumber" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item label="验证码" prop="captcha">
            <div class="login-middle-mobile-captcha">
              <el-input v-model="mobileLoginForm.captcha" placeholder="请输入验证码" />
              <el-button type="primary" class="login-middle-mobile-captcha-button"
                >获取验证码</el-button
              >
            </div>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <div class="login-bottom">
      <el-checkbox v-model="isRememberPassWord" label="记住密码" />
      <el-link type="primary" underline="never">忘记密码</el-link>
    </div>
    <el-button type="primary" class="login-button" large @click="loginNow(accountLoginFormRef)"
      >立即登录</el-button
    >
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { FormRules, FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../../stores/modules/user'
import { localCache } from '../../utils/cache'
import type { LoginInfo } from '../../types'

const USER_NAME = 'username'
const PASSWORD = 'password'
const IS_REM_PASS = 'isRememberPassWord'
let isRememberPassWord = ref<boolean>(localCache.getCache(IS_REM_PASS) || false)
let loginMethod = ref('accountLogin')
let accountLoginForm = ref<LoginInfo>({
  username: localCache.getCache(USER_NAME) || '',
  password: localCache.getCache(PASSWORD) || '',
})
let mobileLoginForm = ref({
  mobileNumber: '',
  captcha: '',
})
let accountLoginFormRef = ref()
let accountLoginRules = reactive<FormRules<any>>({
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})
let mobileLoginRules = reactive<FormRules<any>>({
  mobileNumber: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'change' },
  ],
  captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
})
const userStore = useUserStore()
const loginNow = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      switch (loginMethod.value) {
        case 'accountLogin':
          const userName = accountLoginForm.value.username
          const password = accountLoginForm.value.password
          userStore.login({ userId: userName, password }).then((res) => {
            if (isRememberPassWord.value) {
              localCache.setCache(USER_NAME, userName)
              localCache.setCache(PASSWORD, password)
              localCache.setCache(IS_REM_PASS, isRememberPassWord.value)
            } else {
              localCache.removeCache(USER_NAME)
              localCache.removeCache(PASSWORD)
              localCache.removeCache(IS_REM_PASS)
            }
          })
          break
        case 'mobileLogin':
          console.log('手机登录')
          break
      }
    } else {
      ElMessage.error('请填写完整信息')
    }
  })
}
</script>

<style scoped lang="less">
.login {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url('@/assets/images/bg-login.jpg');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .login-top {
    font-size: 28px;
    font-weight: bold;
    padding: 10px;
  }
  .login-middle {
    width: 300px;
    height: 160px;
    box-shadow: 0 5px 10px 8px rgba(137, 137, 137, 0.5);
    .login-middle-icon {
      margin-right: 5px;
    }
    .login-middle-mobile-captcha {
      display: flex;
      .login-middle-mobile-captcha-button {
        margin-left: 5px;
        width: 77px;
      }
    }
  }
  .login-bottom {
    width: 300px;
    display: flex;
    justify-content: space-between;
    padding: 5px 0px;
    .el-checkbox {
      color: #e1e1e1;
    }
  }
  .login-button {
    width: 300px;
    height: 45px;
  }
}
</style>
